package com.ruoyi.ma.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.io.resource.ClassPathResource;
import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.deepoove.poi.XWPFTemplate;
import com.deepoove.poi.config.Configure;
import com.deepoove.poi.plugin.table.LoopRowTableRenderPolicy;
import com.ruoyi.base.domain.BaseBsBd;
import com.ruoyi.base.domain.BaseBsProject;
import com.ruoyi.base.domain.BaseBsZd;
import com.ruoyi.base.domain.vo.BaseBsProjectVo;
import com.ruoyi.base.mapper.BaseBsBdMapper;
import com.ruoyi.base.mapper.BaseBsProjectMapper;
import com.ruoyi.base.mapper.BaseBsZdMapper;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.ma.domain.*;
import com.ruoyi.ma.domain.vo.MaMaterialMeasureSingleVo;
import com.ruoyi.ma.mapper.*;
import com.ruoyi.util.HandleFileUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.ma.domain.bo.MaReportFormPaymentBo;
import com.ruoyi.ma.domain.vo.MaReportFormPaymentVo;
import com.ruoyi.ma.service.IMaReportFormPaymentService;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 往期材料支付报表Service业务层处理
 *
 * @author tll
 * @date 2024-11-27
 */
@RequiredArgsConstructor
@Service
public class MaReportFormPaymentServiceImpl implements IMaReportFormPaymentService {

    private final MaReportFormPaymentMapper baseMapper;
    private final BaseBsBdMapper bdMapper;
    private final BaseBsProjectMapper projectMapper;
    private final BaseBsZdMapper zdMapper;
    private final MaMaterialSupplierMapper supplierMapper;
    private final MaMaterialMeasureSingleMapper singleMapper;
    private final MaMaterialOtherMeasureMapper otherMapper;
    private final MaMaterialManifestMapper manifestMapper;

    /**
     * 查询往期材料支付报表
     */
    @Override
    public MaReportFormPaymentVo queryById(Long reportId){
        return baseMapper.selectVoById(reportId);
    }

    /**
     * 查询往期材料支付报表列表
     */
    @Override
    public TableDataInfo<MaReportFormPaymentVo> queryPageList(MaReportFormPaymentBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<MaReportFormPayment> lqw = buildQueryWrapper(bo);
        lqw.orderByDesc(MaReportFormPayment::getQh);
        Page<MaReportFormPaymentVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询往期材料支付报表列表
     */
    @Override
    public List<MaReportFormPaymentVo> queryList(MaReportFormPaymentBo bo) {
        LambdaQueryWrapper<MaReportFormPayment> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<MaReportFormPayment> buildQueryWrapper(MaReportFormPaymentBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<MaReportFormPayment> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getBdId() != null, MaReportFormPayment::getBdId, bo.getBdId());
        lqw.eq(bo.getZdId() != null, MaReportFormPayment::getZdId, bo.getZdId());
        lqw.eq(bo.getGysId() != null, MaReportFormPayment::getGysId, bo.getGysId());
        lqw.eq(bo.getType() != null, MaReportFormPayment::getType, bo.getType());
        lqw.eq(bo.getMeasureQhId() != null, MaReportFormPayment::getMeasureQhId, bo.getMeasureQhId());
        lqw.eq(bo.getQh() != null, MaReportFormPayment::getQh, bo.getQh());
        lqw.like(StringUtils.isNotBlank(bo.getQhName()), MaReportFormPayment::getQhName, bo.getQhName());
        lqw.eq(StringUtils.isNotBlank(bo.getCurrentQhFlag()), MaReportFormPayment::getCurrentQhFlag, bo.getCurrentQhFlag());
        lqw.eq(bo.getBeginDate() != null, MaReportFormPayment::getBeginDate, bo.getBeginDate());
        lqw.eq(bo.getEndDate() != null, MaReportFormPayment::getEndDate, bo.getEndDate());
        lqw.eq(StringUtils.isNotBlank(bo.getWorkflowsStateus()), MaReportFormPayment::getWorkflowsStateus, bo.getWorkflowsStateus());
        return lqw;
    }

    /**
     * 新增往期材料支付报表
     */
    @Override
    public Boolean insertByBo(MaReportFormPaymentBo bo) {
        MaReportFormPayment add = BeanUtil.toBean(bo, MaReportFormPayment.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setReportId(add.getReportId());
        }
        return flag;
    }

    /**
     * 修改往期材料支付报表
     */
    @Override
    public Boolean updateByBo(MaReportFormPaymentBo bo) {
        MaReportFormPayment update = BeanUtil.toBean(bo, MaReportFormPayment.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(MaReportFormPayment entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除往期材料支付报表
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    // 导出封面
    @Override
    public void importFm(HttpServletResponse response, MaReportFormPaymentBo bo) {
        // 获取组织机构信息
        Map<String, Object> deptInfo = getDeptInfo(bo.getReportId());
        String bdmc = (String) (ObjectUtils.isEmpty(deptInfo.get("bdmc")) ? "": deptInfo.get("bdmc"));
        String zdmc = (String) (ObjectUtils.isEmpty(deptInfo.get("zdmc")) ? "": deptInfo.get("zdmc"));
        String qh = (String) (ObjectUtils.isEmpty(deptInfo.get("qh")) ? "": deptInfo.get("qh"));
        String zfqh = bdmc + "-" + zdmc + "-" + qh;

        // 指定word模板
        InputStream wordTemplate = HandleFileUtil.getWordTemplate("excel/材料支付封面.docx");
        // 设置word内容
        Map<String, Object> finalMap = new HashMap<>();
        finalMap.put("sgdw", deptInfo.get("sgdw")); // 施工单位
        finalMap.put("zfqh", zfqh); // 支付期号
        finalMap.put("bzdw", deptInfo.get("gys")); // 编制单位
        finalMap.put("zdb", deptInfo.get("jldw")); // 驻地办
        finalMap.put("zjb", deptInfo.get("jldw")); // 总监办
        finalMap.put("rq", deptInfo.get("jzrq")); // 日期
        finalMap.put("xmmc", deptInfo.get("xmmc")); // 项目名称
        finalMap.put("xmyz", deptInfo.get("xmyz")); // 项目业主
        List<Object> workList = new ArrayList<>();

        finalMap.put("workList", workList);
        HandleFileUtil.writeToWord(response, wordTemplate, finalMap);
    }

    // 获取报表信息
    public MaReportFormPayment getReportData(Long reportId) {
        LambdaQueryWrapper<MaReportFormPayment> lqw = Wrappers.lambdaQuery();
        lqw.eq(reportId != null, MaReportFormPayment::getReportId, reportId);
        MaReportFormPayment bean = baseMapper.selectOne(lqw);
        return bean;
    }

    // 导出支付申请
    @Override
    public void importZfsq(HttpServletResponse response, MaReportFormPaymentBo bo) {
        // 获取组织机构信息
        Map<String, Object> deptInfo = getDeptInfo(bo.getReportId());
        // 获取报表信息
        MaReportFormPayment bean = getReportData(bo.getReportId());
        // 指定word模板
        InputStream wordTemplate = HandleFileUtil.getWordTemplate("excel/材料支付申请表.docx");
        // 设置word内容
        Map<String, Object> finalMap = new HashMap<>();
        finalMap.put("projectName", deptInfo.get("xmmc")); // 项目名称
        finalMap.put("gysName", deptInfo.get("gys")); // 供应商
        finalMap.put("qh", deptInfo.get("qh")); // 期号
        finalMap.put("cbdw", deptInfo.get("sgdw")); // 承包单位
        finalMap.put("xmyz", deptInfo.get("xmyz")); // 项目业主
        finalMap.put("jldw", deptInfo.get("jldw")); // 监理单位
        finalMap.put("jzrq", deptInfo.get("jzrq")); // 截止日期
        finalMap.put("zjb", ""); // 总监办
        finalMap.put("zdb", ""); // 驻地办
        finalMap.put("sgdw", ""); // 施工单位
        finalMap.put("zbr", ""); // 制表人

//        List<Map<String, Object>> workList = new ArrayList<>();
//        String[] arr = {"标志牌", "项目小计", "索赔金额", "违约罚金", "迟付款利息", "其他", "合计", "扣留质保金", "到期应退还质保金", "支付"};
//        for (int i=0; i<10; i++) {
//            String var = "";
//            Map<String, Object> workItem = new HashMap<>();
//            workItem.put("subItemType", arr[i]);
//            workItem.put("clPlanCgNum", var);
//            workItem.put("clBeChgNum", var);
//            workItem.put("clAfChgNum", var);
//            workItem.put("bqmNum", 5504.436);
//            workItem.put("bqmTotal", 9938590.66);
//            workItem.put("bqmPerNum", var);
//            workItem.put("sqmNum", 5366.910);
//            workItem.put("sqmTotal", 9846341.96);
//            workItem.put("bqNum", 137.526);
//            workItem.put("bqTotal", 92248.70);
//            workItem.put("bqPerNum", var);
//            workList.add(workItem);
//        }

        List<Map<String, Object>> workList = getZfsqData(bean.getReportId(), bean.getGysId());
        finalMap.put("workList", workList);
        HandleFileUtil.writeToWord(response, wordTemplate, finalMap);
    }

    // 导出支付明细
    @Override
    public void importZfmx(HttpServletResponse response, MaReportFormPaymentBo bo) {
        // 获取组织机构信息
        Map<String, Object> deptInfo = getDeptInfo(bo.getReportId());
        // 获取报表信息
        MaReportFormPayment bean = getReportData(bo.getReportId());
        // 指定word模板
        InputStream wordTemplate = HandleFileUtil.getWordTemplate("excel/材料支付明细表.docx");
        // 设置word内容
        Map<String, Object> finalMap = new HashMap<>();
        finalMap.put("projectName", deptInfo.get("xmmc")); // 项目名称
        finalMap.put("gysName", deptInfo.get("gys")); // 供应商
        finalMap.put("qh", deptInfo.get("qh")); // 期号
        finalMap.put("cbdw", deptInfo.get("sgdw")); // 承包单位
        finalMap.put("xmyz", deptInfo.get("xmyz")); // 项目业主
        finalMap.put("jldw", deptInfo.get("jldw")); // 监理单位
        finalMap.put("jzrq", deptInfo.get("jzrq")); // 截止日期

//        List<Object> workList = new ArrayList<>();
//        String[] arr = {"标志牌", "门式钢支架（含立柱、横梁、法兰盘、加劲肋、柱帽、抱箍及底衬等）",
//            "标志板（含铝板、滑动槽铝、角铝等）", "反光膜", "光伏组件"};
//        for (int i=0; i<5; i++) {
//            String var = "";
//            Map<String, Object> workItem = new HashMap<>();
//            workItem.put("manifestCode", "600-"+(i+1));
//            workItem.put("manifestName", arr[i]);
//            workItem.put("clPlanCgNum", var);
//            workItem.put("clBeChgNum", var);
//            workItem.put("clAfChgNum", var);
//            workItem.put("bqmNum", 5504.436);
//            workItem.put("bqmTotal", 9938590.66);
//            workItem.put("bqmPerNum", var);
//            workItem.put("sqmNum", 5366.910);
//            workItem.put("sqmTotal", 9846341.96);
//            workItem.put("bqNum", 137.526);
//            workItem.put("bqTotal", 92248.70);
//            workItem.put("bqPerNum", var);
//            workList.add(workItem);
//        }

        List<Map<String, Object>> workList = getZfmxData(bean.getReportId(), bean.getGysId());
        finalMap.put("workList", workList);
        HandleFileUtil.writeToWord(response, wordTemplate, finalMap);
    }

    // 导出月度供货
    @Override
    public void importYdgh(HttpServletResponse response, MaReportFormPaymentBo bo) {
        // 获取组织机构信息
        Map<String, Object> deptInfo = getDeptInfo(bo.getReportId());
        // 获取报表信息
        MaReportFormPayment bean = getReportData(bo.getReportId());
        // 指定word模板
        InputStream wordTemplate = HandleFileUtil.getWordTemplate("excel/材料月度供货费用计算表.docx");
        // 设置word内容
        Map<String, Object> finalMap = new HashMap<>();
        finalMap.put("projectName", deptInfo.get("xmmc")); // 项目名称
        finalMap.put("gysName", deptInfo.get("gys")); // 供应商
        finalMap.put("qh", deptInfo.get("qh")); // 期号
        finalMap.put("cbdw", deptInfo.get("sgdw")); // 承包单位
        finalMap.put("xmyz", deptInfo.get("xmyz")); // 项目业主
        finalMap.put("jldw", deptInfo.get("jldw")); // 监理单位
        finalMap.put("jzrq", deptInfo.get("jzrq")); // 截止日期

//        List<Object> workList = new ArrayList<>();
//        String[] arr = {"标志牌", "门式钢支架（含立柱、横梁、法兰盘、加劲肋、柱帽、抱箍及底衬等）",
//            "标志板（含铝板、滑动槽铝、角铝等）", "反光膜", "光伏组件"};
//        for (int i=0; i<5; i++) {
//            String var = "";
//            Map<String, Object> workItem = new HashMap<>();
//            workItem.put("manifestCode", "600-"+(i+1));
//            workItem.put("manifestName", arr[i]);
//            workItem.put("number", 123);
//            workItem.put("marketPrice", 10695.00);
//            workItem.put("adjustments", 0.00);
//            workItem.put("settlementPrice", 10695.00);
//            workItem.put("total", 23240.24);
//            workItem.put("acceptanceCode", 	1491);
//            workItem.put("remark", var);
//            workList.add(workItem);
//        }

        List<Map<String, Object>> workList = getYdghData(bean.getReportId(), bean.getGysId());
        finalMap.put("workList", workList);
        HandleFileUtil.writeToWord(response, wordTemplate, finalMap);
    }

    // 获取往期材料支付报表详细信息
    @Override
    public MaReportFormPaymentVo detail(MaReportFormPaymentBo bo) {
        LambdaQueryWrapper<MaReportFormPayment> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getGysId() != null, MaReportFormPayment::getGysId, bo.getGysId());
        lqw.eq(bo.getMeasureQhId() != null, MaReportFormPayment::getMeasureQhId, bo.getMeasureQhId());
        return baseMapper.selectVoOne(lqw);
    }

    /**
     * 支付情况统计明细
     *
     * @param bo
     * @return
     */
    @Override
    public Map<String, Object> statistics(MaReportFormPaymentBo bo) {
//        Map<String, Object> data = new HashMap<>();
//        List<String> header = new ArrayList<>();
//        List<Object> list = new ArrayList<>();
//        List<Object> total = new ArrayList<>();
//
//        data.put("header", header);
//        data.put("list", list);
//        data.put("total", total);

        return zftj(bo.getProjectId());
    }


    // 根据报表id获取标段信息
    public Map<String, Object> getDeptInfo(Long reportId) {
        LambdaQueryWrapper<MaReportFormPayment> lqw = Wrappers.lambdaQuery();
        lqw.eq(reportId != null, MaReportFormPayment::getReportId, reportId);
        MaReportFormPayment bean = baseMapper.selectOne(lqw);

        LambdaQueryWrapper<BaseBsBd> bdLqw = Wrappers.lambdaQuery();
        bdLqw.eq(bean.getBdId() != null, BaseBsBd::getId, bean.getBdId());
        BaseBsBd baseBsBd = bdMapper.selectOne(bdLqw);

        //查询项目的所属单位；
        BaseBsProjectVo projectVo = projectMapper.selectVoOne(new LambdaQueryWrapper<BaseBsProject>().eq(BaseBsProject::getProjectId, baseBsBd.getProjectId()));

        LambdaQueryWrapper<BaseBsZd> zdLqw = Wrappers.lambdaQuery();
        zdLqw.eq(bean.getZdId() != null, BaseBsZd::getId, bean.getZdId());
        BaseBsZd baseBsZd = zdMapper.selectOne(zdLqw);

        LambdaQueryWrapper<MaMaterialSupplier> suLqw = Wrappers.lambdaQuery();
        suLqw.eq(bean.getGysId() != null, MaMaterialSupplier::getSupplierId, bean.getGysId());
        MaMaterialSupplier supplier = supplierMapper.selectOne(suLqw);

        Map<String, Object> data = new HashMap<>();
        data.put("bdmc", baseBsBd.getName()); // 标段名称
        data.put("xmmc", baseBsBd.getProjectName()); // 项目名称
        data.put("xmyz", projectVo.getDeptName()); // 项目业主
        data.put("zdmc", baseBsZd.getName()); // 站点名称
        data.put("sgdw", baseBsBd.getSgdwName()); // 施工单位名称
        data.put("jldw", baseBsBd.getJldwName()); // 监理单位名称
        data.put("gys", supplier.getDeptName()); // 供应商名称
        data.put("qh", bean.getQhName()); // 期号名称
        // 日期处理
        if (ObjectUtils.isNotEmpty(bean.getEndDate())) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            String format = sdf.format(bean.getEndDate());
            data.put("jzrq", format); // 截止日期
        } else {
            data.put("jzrq", ""); // 截止日期
        }
        return data;
    }

    // 获取支付报表信息
    public MaReportFormPayment getReportData(Long reportId, Long gysId) {
        LambdaQueryWrapper<MaReportFormPayment> lqw = Wrappers.lambdaQuery();
        lqw.eq(reportId != null, MaReportFormPayment::getReportId, reportId);
        MaReportFormPayment bean = baseMapper.selectById(reportId);
        return bean;
    }

    // 获取本期对应计量单信息
    public List<MaMaterialMeasureSingle> getMeasureData(Long reportId, Long gysId, boolean isCurrQh) {
        MaReportFormPayment reportData = getReportData(reportId, gysId);
        LambdaQueryWrapper<MaMaterialMeasureSingle> lqw = Wrappers.lambdaQuery();
        lqw.eq(gysId != null, MaMaterialMeasureSingle::getGysId, gysId);
        if (isCurrQh) {
            lqw.eq(reportData.getMeasureQhId() != null, MaMaterialMeasureSingle::getMeasureQhId, reportData.getMeasureQhId());
        }
        List<MaMaterialMeasureSingle> data = singleMapper.selectList(lqw);
        return data;
    }
    // 获取对应其他计量信息
    public List<MaMaterialOtherMeasure> getOtherMeasure(Long reportId, Long gysId, boolean isCurrQh) {
        MaReportFormPayment reportData = getReportData(reportId, gysId);
        LambdaQueryWrapper<MaMaterialOtherMeasure> lqw = Wrappers.lambdaQuery();
        lqw.eq(gysId != null,MaMaterialOtherMeasure::getGysId, gysId);
        if (isCurrQh) {
            lqw.eq(reportData.getMeasureQhId() != null, MaMaterialOtherMeasure::getMeasureQhId, reportData.getMeasureQhId());
        }
        List<MaMaterialOtherMeasure> data = otherMapper.selectList(lqw);
        return data;
    }
    // 获取供应商信息
    public MaMaterialSupplier getSupplierData(Long reportId, Long gysId) {
        LambdaQueryWrapper<MaMaterialSupplier> lqw = Wrappers.lambdaQuery();
        lqw.eq(gysId != null, MaMaterialSupplier::getSupplierId, gysId);
        MaMaterialSupplier supplier = supplierMapper.selectOne(lqw);
        return supplier;
    }

    // 配置支付申请
    public List<Map<String, Object>> getZfsqData(Long reportId, Long gysId) {
        // 计量单-本期完成
        List<MaMaterialMeasureSingle> currMeasureData = getMeasureData(reportId, gysId, true);
        List<BigDecimal> meNumList1 = currMeasureData.stream().map(MaMaterialMeasureSingle::getMeNum).collect(Collectors.toList());
        List<BigDecimal> meTotalList1 = currMeasureData.stream().map(MaMaterialMeasureSingle::getMeTotal).collect(Collectors.toList());

        BigDecimal meNumSum1 = meNumList1.stream().filter(Objects::nonNull).reduce(BigDecimal.ZERO, BigDecimal::add);
        BigDecimal meTotalSum1 = meTotalList1.stream().filter(Objects::nonNull).reduce(BigDecimal.ZERO, BigDecimal::add);
        BigDecimal per1 = null;

        // 计量单-到本期末完成
        List<MaMaterialMeasureSingle> totalMeasureData = getMeasureData(reportId, gysId, false);
        List<BigDecimal> meNumList2 = totalMeasureData.stream().map(MaMaterialMeasureSingle::getMeNum).collect(Collectors.toList());
        List<BigDecimal> meTotalList2 = totalMeasureData.stream().map(MaMaterialMeasureSingle::getMeTotal).collect(Collectors.toList());

        BigDecimal meNumSum2 = meNumList2.stream().filter(Objects::nonNull).reduce(BigDecimal.ZERO, BigDecimal::add);
        BigDecimal meTotalSum2 = meTotalList2.stream().filter(Objects::nonNull).reduce(BigDecimal.ZERO, BigDecimal::add);
        BigDecimal per2 = null;

        // 计量单-到上期末完成
        BigDecimal meNumSum3 = null;
        if (ObjectUtils.isNotEmpty(meNumSum1) && ObjectUtils.isNotEmpty(meNumSum2)) {
            meNumSum3 = meNumSum2.subtract(meNumSum1);
        }
        BigDecimal meTotalSum3 = null;
        if (ObjectUtils.isNotEmpty(meTotalSum1) && ObjectUtils.isNotEmpty(meTotalSum2)) {
            meTotalSum3 = meTotalSum2.subtract(meTotalSum1);
        }

        // 其他计量-本期完成
        List<MaMaterialOtherMeasure> other1 = getOtherMeasure(reportId, gysId, true);
        List<BigDecimal> spjrList1 = other1.stream().map(MaMaterialOtherMeasure::getSpjr).collect(Collectors.toList());
        List<BigDecimal> wyfjList1 = other1.stream().map(MaMaterialOtherMeasure::getWyfj).collect(Collectors.toList());
        List<BigDecimal> cfklxList1 = other1.stream().map(MaMaterialOtherMeasure::getCfklx).collect(Collectors.toList());
        List<BigDecimal> otherList1 = other1.stream().map(MaMaterialOtherMeasure::getOther).collect(Collectors.toList());
        List<BigDecimal> klzbjList1 = other1.stream().map(MaMaterialOtherMeasure::getKlzbj).collect(Collectors.toList());
        List<BigDecimal> dqythbzjList1 = other1.stream().map(MaMaterialOtherMeasure::getDqythbzj).collect(Collectors.toList());

        BigDecimal spjrSum1 = spjrList1.stream().filter(Objects::nonNull).reduce(BigDecimal.ZERO, BigDecimal::add);
        BigDecimal wyfjSum1 = wyfjList1.stream().filter(Objects::nonNull).reduce(BigDecimal.ZERO, BigDecimal::add);
        BigDecimal cfklxSum1 = cfklxList1.stream().filter(Objects::nonNull).reduce(BigDecimal.ZERO, BigDecimal::add);
        BigDecimal otherSum1 = otherList1.stream().filter(Objects::nonNull).reduce(BigDecimal.ZERO, BigDecimal::add);
        BigDecimal klzbjSum1 = klzbjList1.stream().filter(Objects::nonNull).reduce(BigDecimal.ZERO, BigDecimal::add);
        BigDecimal dqythbzjSum1 = dqythbzjList1.stream().filter(Objects::nonNull).reduce(BigDecimal.ZERO, BigDecimal::add);

        // 合计-本期完成
        List<BigDecimal> hjList1 = new ArrayList<>();
        hjList1.add(spjrSum1);
        hjList1.add(wyfjSum1);
        hjList1.add(cfklxSum1);
        hjList1.add(otherSum1);
        hjList1.add(klzbjSum1);
        hjList1.add(dqythbzjSum1);
        BigDecimal hjSum1 = hjList1.stream().filter(Objects::nonNull).reduce(BigDecimal.ZERO, BigDecimal::add);

        // 其他计量-到本期末完成
        List<MaMaterialOtherMeasure> other2 = getOtherMeasure(reportId, gysId, false);
        List<BigDecimal> spjrList2 = other2.stream().map(MaMaterialOtherMeasure::getSpjr).collect(Collectors.toList());
        List<BigDecimal> wyfjList2 = other2.stream().map(MaMaterialOtherMeasure::getWyfj).collect(Collectors.toList());
        List<BigDecimal> cfklxList2 = other2.stream().map(MaMaterialOtherMeasure::getCfklx).collect(Collectors.toList());
        List<BigDecimal> otherList2 = other2.stream().map(MaMaterialOtherMeasure::getOther).collect(Collectors.toList());
        List<BigDecimal> klzbjList2 = other2.stream().map(MaMaterialOtherMeasure::getKlzbj).collect(Collectors.toList());
        List<BigDecimal> dqythbzjList2 = other2.stream().map(MaMaterialOtherMeasure::getDqythbzj).collect(Collectors.toList());

        BigDecimal spjrSum2 = spjrList2.stream().filter(Objects::nonNull).reduce(BigDecimal.ZERO, BigDecimal::add);
        BigDecimal wyfjSum2 = wyfjList2.stream().filter(Objects::nonNull).reduce(BigDecimal.ZERO, BigDecimal::add);
        BigDecimal cfklxSum2 = cfklxList2.stream().filter(Objects::nonNull).reduce(BigDecimal.ZERO, BigDecimal::add);
        BigDecimal otherSum2 = otherList2.stream().filter(Objects::nonNull).reduce(BigDecimal.ZERO, BigDecimal::add);
        BigDecimal klzbjSum2 = klzbjList2.stream().filter(Objects::nonNull).reduce(BigDecimal.ZERO, BigDecimal::add);
        BigDecimal dqythbzjSum2 = dqythbzjList2.stream().filter(Objects::nonNull).reduce(BigDecimal.ZERO, BigDecimal::add);

        // 合计-到本期末完成
        List<BigDecimal> hjList2 = new ArrayList<>();
        hjList2.add(spjrSum2);
        hjList2.add(wyfjSum2);
        hjList2.add(cfklxSum2);
        hjList2.add(otherSum2);
        hjList2.add(klzbjSum2);
        hjList2.add(dqythbzjSum2);
        BigDecimal hjSum2 = hjList2.stream().filter(Objects::nonNull).reduce(BigDecimal.ZERO, BigDecimal::add);

        // 其他计量-到上期末完成
        BigDecimal spjrSum3 = null;
        if (ObjectUtils.isNotEmpty(spjrSum1) && ObjectUtils.isNotEmpty(spjrSum2)) {
            spjrSum3 = spjrSum2.subtract(spjrSum1);
        }
        BigDecimal wyfjSum3 = null;
        if (ObjectUtils.isNotEmpty(wyfjSum1) && ObjectUtils.isNotEmpty(wyfjSum2)) {
            wyfjSum3 = wyfjSum2.subtract(wyfjSum1);
        }
        BigDecimal cfklxSum3 = null;
        if (ObjectUtils.isNotEmpty(cfklxSum1) && ObjectUtils.isNotEmpty(cfklxSum2)) {
            cfklxSum3 = cfklxSum2.subtract(cfklxSum1);
        }
        BigDecimal otherSum3 = null;
        if (ObjectUtils.isNotEmpty(otherSum1) && ObjectUtils.isNotEmpty(otherSum2)) {
            otherSum3 = otherSum2.subtract(otherSum1);
        }
        BigDecimal klzbjSum3 = null;
        if (ObjectUtils.isNotEmpty(klzbjSum1) && ObjectUtils.isNotEmpty(klzbjSum2)) {
            klzbjSum3 = klzbjSum2.subtract(klzbjSum1);
        }
        BigDecimal dqythbzjSum3 = null;
        if (ObjectUtils.isNotEmpty(dqythbzjSum1) && ObjectUtils.isNotEmpty(dqythbzjSum2)) {
            dqythbzjSum3 = dqythbzjSum2.subtract(dqythbzjSum1);
        }

        // 合计-到上期末完成
        List<BigDecimal> hjList3 = new ArrayList<>();
        hjList3.add(spjrSum3);
        hjList3.add(wyfjSum3);
        hjList3.add(cfklxSum3);
        hjList3.add(otherSum3);
        hjList3.add(klzbjSum3);
        hjList3.add(dqythbzjSum3);
        BigDecimal hjSum3 = hjList3.stream().filter(Objects::nonNull).reduce(BigDecimal.ZERO, BigDecimal::add);

        // 供应商
        MaMaterialSupplier supplierData = getSupplierData(reportId, gysId);
        String typeName = supplierData.getGhcl();

        // 列项
        List<String> arr = new ArrayList<>();
        arr.add(typeName);
        arr.add("项目小计");
        arr.add("违约罚金");
        arr.add("索赔金额");
        arr.add("迟付款利息");
        arr.add("其他");
        arr.add("合计");
        arr.add("扣留质保金");
        arr.add("到期应退还质保金");
        arr.add("支付");

        // 内容
        List<Map<String, Object>> workList = new ArrayList<>();

        Map<String, Object> map1 = new HashMap<>();
        map1.put("subItemType", arr.get(0)); // 项目名称
        map1.put("clPlanCgNum", null); // 计划采购数量
        map1.put("clBeChgNum", null); // 变更数量
        map1.put("clAfChgNum", null); // 变更后数量
        map1.put("bqmNum", meNumSum2); // 到本期末完成数量
        map1.put("bqmTotal", meTotalSum2); // 到本期末完成金额
        map1.put("bqmPerNum", per2); // 到本期末完成占比
        map1.put("sqmNum", meNumSum3); // 到上期末完成数量
        map1.put("sqmTotal", meTotalSum3); // 到上期末完成金额
        map1.put("bqNum", meNumSum1); // 本期完成数量
        map1.put("bqTotal", meTotalSum1); // 本期完成金额
        map1.put("bqPerNum", per1); // 本期完成占比
        workList.add(map1);

        Map<String, Object> map2 = new HashMap<>();
        map2.put("subItemType", arr.get(1)); // 项目名称
        map2.put("clPlanCgNum", null); // 计划采购数量
        map2.put("clBeChgNum", null); // 变更数量
        map2.put("clAfChgNum", null); // 变更后数量
        map2.put("bqmNum", meNumSum2); // 到本期末完成数量
        map2.put("bqmTotal", meTotalSum2); // 到本期末完成金额
        map2.put("bqmPerNum", per2); // 到本期末完成占比
        map2.put("sqmNum", meNumSum3); // 到上期末完成数量
        map2.put("sqmTotal", meTotalSum3); // 到上期末完成金额
        map2.put("bqNum", meNumSum1); // 本期完成数量
        map2.put("bqTotal", meTotalSum1); // 本期完成金额
        map2.put("bqPerNum", per1); // 本期完成占比
        workList.add(map2);

        Map<String, Object> map3 = new HashMap<>();
        map3.put("subItemType", arr.get(2)); // 项目名称
        map3.put("clPlanCgNum", null); // 计划采购数量
        map3.put("clBeChgNum", null); // 变更数量
        map3.put("clAfChgNum", null); // 变更后数量
        map3.put("bqmNum", meNumSum2); // 到本期末完成数量
        map3.put("bqmTotal", spjrSum2); // 到本期末完成金额
        map3.put("bqmPerNum", per2); // 到本期末完成占比
        map3.put("sqmNum", meNumSum3); // 到上期末完成数量
        map3.put("sqmTotal", spjrSum3); // 到上期末完成金额
        map3.put("bqNum", meNumSum1); // 本期完成数量
        map3.put("bqTotal", spjrSum1); // 本期完成金额
        map3.put("bqPerNum", per1); // 本期完成占比
        workList.add(map3);

        Map<String, Object> map4 = new HashMap<>();
        map4.put("subItemType", arr.get(3)); // 项目名称
        map4.put("clPlanCgNum", null); // 计划采购数量
        map4.put("clBeChgNum", null); // 变更数量
        map4.put("clAfChgNum", null); // 变更后数量
        map4.put("bqmNum", meNumSum2); // 到本期末完成数量
        map4.put("bqmTotal", wyfjSum2); // 到本期末完成金额
        map4.put("bqmPerNum", per2); // 到本期末完成占比
        map4.put("sqmNum", meNumSum3); // 到上期末完成数量
        map4.put("sqmTotal", wyfjSum3); // 到上期末完成金额
        map4.put("bqNum", meNumSum1); // 本期完成数量
        map4.put("bqTotal", wyfjSum1); // 本期完成金额
        map4.put("bqPerNum", per1); // 本期完成占比
        workList.add(map4);

        Map<String, Object> map5 = new HashMap<>();
        map5.put("subItemType", arr.get(4)); // 项目名称
        map5.put("clPlanCgNum", null); // 计划采购数量
        map5.put("clBeChgNum", null); // 变更数量
        map5.put("clAfChgNum", null); // 变更后数量
        map5.put("bqmNum", meNumSum2); // 到本期末完成数量
        map5.put("bqmTotal", cfklxSum2); // 到本期末完成金额
        map5.put("bqmPerNum", per2); // 到本期末完成占比
        map5.put("sqmNum", meNumSum3); // 到上期末完成数量
        map5.put("sqmTotal", cfklxSum3); // 到上期末完成金额
        map5.put("bqNum", meNumSum1); // 本期完成数量
        map5.put("bqTotal", cfklxSum1); // 本期完成金额
        map5.put("bqPerNum", per1); // 本期完成占比
        workList.add(map5);

        Map<String, Object> map6 = new HashMap<>();
        map6.put("subItemType", arr.get(5)); // 项目名称
        map6.put("clPlanCgNum", null); // 计划采购数量
        map6.put("clBeChgNum", null); // 变更数量
        map6.put("clAfChgNum", null); // 变更后数量
        map6.put("bqmNum", meNumSum2); // 到本期末完成数量
        map6.put("bqmTotal", otherSum2); // 到本期末完成金额
        map6.put("bqmPerNum", per2); // 到本期末完成占比
        map6.put("sqmNum", meNumSum3); // 到上期末完成数量
        map6.put("sqmTotal", otherSum3); // 到上期末完成金额
        map6.put("bqNum", meNumSum1); // 本期完成数量
        map6.put("bqTotal", otherSum1); // 本期完成金额
        map6.put("bqPerNum", per1); // 本期完成占比
        workList.add(map6);

        Map<String, Object> map7 = new HashMap<>();
        map7.put("subItemType", arr.get(6)); // 项目名称
        map7.put("clPlanCgNum", null); // 计划采购数量
        map7.put("clBeChgNum", null); // 变更数量
        map7.put("clAfChgNum", null); // 变更后数量
        map7.put("bqmNum", meNumSum2); // 到本期末完成数量
        map7.put("bqmTotal", hjSum2); // 到本期末完成金额
        map7.put("bqmPerNum", per2); // 到本期末完成占比
        map7.put("sqmNum", meNumSum3); // 到上期末完成数量
        map7.put("sqmTotal", hjSum3); // 到上期末完成金额
        map7.put("bqNum", meNumSum1); // 本期完成数量
        map7.put("bqTotal", hjSum1); // 本期完成金额
        map7.put("bqPerNum", per1); // 本期完成占比
        workList.add(map7);

        Map<String, Object> map8 = new HashMap<>();
        map8.put("subItemType", arr.get(7)); // 项目名称
        map8.put("clPlanCgNum", null); // 计划采购数量
        map8.put("clBeChgNum", null); // 变更数量
        map8.put("clAfChgNum", null); // 变更后数量
        map8.put("bqmNum", meNumSum2); // 到本期末完成数量
        map8.put("bqmTotal", klzbjSum2); // 到本期末完成金额
        map8.put("bqmPerNum", per2); // 到本期末完成占比
        map8.put("sqmNum", meNumSum3); // 到上期末完成数量
        map8.put("sqmTotal", klzbjSum3); // 到上期末完成金额
        map8.put("bqNum", meNumSum1); // 本期完成数量
        map8.put("bqTotal", klzbjSum1); // 本期完成金额
        map8.put("bqPerNum", per1); // 本期完成占比
        workList.add(map8);

        Map<String, Object> map9 = new HashMap<>();
        map9.put("subItemType", arr.get(8)); // 项目名称
        map9.put("clPlanCgNum", null); // 计划采购数量
        map9.put("clBeChgNum", null); // 变更数量
        map9.put("clAfChgNum", null); // 变更后数量
        map9.put("bqmNum", meNumSum2); // 到本期末完成数量
        map9.put("bqmTotal", dqythbzjSum2); // 到本期末完成金额
        map9.put("bqmPerNum", per2); // 到本期末完成占比
        map9.put("sqmNum", meNumSum3); // 到上期末完成数量
        map9.put("sqmTotal", dqythbzjSum3); // 到上期末完成金额
        map9.put("bqNum", meNumSum1); // 本期完成数量
        map9.put("bqTotal", dqythbzjSum1); // 本期完成金额
        map9.put("bqPerNum", per1); // 本期完成占比
        workList.add(map9);

        Map<String, Object> map10 = new HashMap<>();
        map10.put("subItemType", arr.get(9)); // 项目名称
        map10.put("clPlanCgNum", null); // 计划采购数量
        map10.put("clBeChgNum", null); // 变更数量
        map10.put("clAfChgNum", null); // 变更后数量
        map10.put("bqmNum", meNumSum2); // 到本期末完成数量
        map10.put("bqmTotal", hjSum2); // 到本期末完成金额
        map10.put("bqmPerNum", per2); // 到本期末完成占比
        map10.put("sqmNum", meNumSum3); // 到上期末完成数量
        map10.put("sqmTotal", hjSum3); // 到上期末完成金额
        map10.put("bqNum", meNumSum1); // 本期完成数量
        map10.put("bqTotal", hjSum1); // 本期完成金额
        map10.put("bqPerNum", per1); // 本期完成占比
        workList.add(map10);

        return workList;
    }

    // 配置支付明细
    public List<Map<String, Object>> getZfmxData(Long reportId, Long gysId) {
        // 计量单-本期完成
        List<MaMaterialMeasureSingle> currMeasureData = getMeasureData(reportId, gysId, true);
        List<BigDecimal> meNumList1 = currMeasureData.stream().map(MaMaterialMeasureSingle::getMeNum).collect(Collectors.toList());
        List<BigDecimal> meTotalList1 = currMeasureData.stream().map(MaMaterialMeasureSingle::getMeTotal).collect(Collectors.toList());

        BigDecimal meNumSum1 = meNumList1.stream().filter(Objects::nonNull).reduce(BigDecimal.ZERO, BigDecimal::add);
        BigDecimal meTotalSum1 = meTotalList1.stream().filter(Objects::nonNull).reduce(BigDecimal.ZERO, BigDecimal::add);
        BigDecimal per1 = null;

        // 按照清单id分组计算-本期完成
        List<Map<String, Object>> details1 = new ArrayList<>();
        Map<Long, List<MaMaterialMeasureSingle>> group1 = currMeasureData.stream().collect(Collectors.groupingBy(MaMaterialMeasureSingle::getManifestId));
        for (Map.Entry map: group1.entrySet()) {
            Long manifestId = (Long) map.getKey();

            List<MaMaterialMeasureSingle> list = (List<MaMaterialMeasureSingle>) map.getValue();
            List<BigDecimal> meNumMapList1 = list.stream().map(MaMaterialMeasureSingle::getMeNum).collect(Collectors.toList());
            List<BigDecimal> meTotalMapList1 = list.stream().map(MaMaterialMeasureSingle::getMeTotal).collect(Collectors.toList());
            BigDecimal meNumMapSum = meNumMapList1.stream().filter(Objects::nonNull).reduce(BigDecimal.ZERO, BigDecimal::add);
            BigDecimal meTotalMapSum = meTotalMapList1.stream().filter(Objects::nonNull).reduce(BigDecimal.ZERO, BigDecimal::add);
            BigDecimal per = null;

            Map<String, Object> sub = new HashMap<>();
            sub.put("manifestId", manifestId);
            sub.put("bqnum", meNumMapSum);
            sub.put("bqtotal", meTotalMapSum);
            sub.put("bqper", per);
            details1.add(sub);
        }

        // 计量单-到本期末完成
        List<MaMaterialMeasureSingle> totalMeasureData = getMeasureData(reportId, gysId, false);
        List<BigDecimal> meNumList2 = totalMeasureData.stream().map(MaMaterialMeasureSingle::getMeNum).collect(Collectors.toList());
        List<BigDecimal> meTotalList2 = totalMeasureData.stream().map(MaMaterialMeasureSingle::getMeTotal).collect(Collectors.toList());

        BigDecimal meNumSum2 = meNumList2.stream().filter(Objects::nonNull).reduce(BigDecimal.ZERO, BigDecimal::add);
        BigDecimal meTotalSum2 = meTotalList2.stream().filter(Objects::nonNull).reduce(BigDecimal.ZERO, BigDecimal::add);
        BigDecimal per2 = null;

        // 按照清单id分组计算-到本期末完成
        List<Map<String, Object>> details2 = new ArrayList<>();
        Map<Long, List<MaMaterialMeasureSingle>> group2 = totalMeasureData.stream().collect(Collectors.groupingBy(MaMaterialMeasureSingle::getManifestId));
        for (Map.Entry map: group2.entrySet()) {
            Long manifestId = (Long) map.getKey();

            List<MaMaterialMeasureSingle> list = (List<MaMaterialMeasureSingle>) map.getValue();
            List<BigDecimal> meNumMapList1 = list.stream().map(MaMaterialMeasureSingle::getMeNum).collect(Collectors.toList());
            List<BigDecimal> meTotalMapList1 = list.stream().map(MaMaterialMeasureSingle::getMeTotal).collect(Collectors.toList());
            BigDecimal meNumMapSum = meNumMapList1.stream().filter(Objects::nonNull).reduce(BigDecimal.ZERO, BigDecimal::add);
            BigDecimal meTotalMapSum = meTotalMapList1.stream().filter(Objects::nonNull).reduce(BigDecimal.ZERO, BigDecimal::add);
            BigDecimal per = null;

            Map<String, Object> sub = new HashMap<>();
            sub.put("manifestId", manifestId);
            sub.put("bqmnum", meNumMapSum);
            sub.put("bqmtotal", meTotalMapSum);
            sub.put("bqmper", per);
            details2.add(sub);
        }

        // 计量单-到上期末完成
        BigDecimal meNumSum3 = null;
        if (ObjectUtils.isNotEmpty(meNumSum1) && ObjectUtils.isNotEmpty(meNumSum2)) {
            meNumSum3 = meNumSum2.subtract(meNumSum1);
        }
        BigDecimal meTotalSum3 = null;
        if (ObjectUtils.isNotEmpty(meTotalSum1) && ObjectUtils.isNotEmpty(meTotalSum2)) {
            meTotalSum3 = meTotalSum2.subtract(meTotalSum1);
        }

        // 合并map
        for (Map<String, Object> map : details2) {
            Long manifestId = (Long) map.get("manifestId");
            LambdaQueryWrapper<MaMaterialManifest> lqw = Wrappers.lambdaQuery();
            lqw.eq(manifestId != null, MaMaterialManifest::getManifestId, manifestId);
            MaMaterialManifest bean = manifestMapper.selectOne(lqw);

            map.put("code", bean.getManifestCode());
            map.put("name", bean.getManifestName());

            Map<String, Object> sub = details1.stream().filter(item -> item.containsKey("manifestId")).findFirst().orElse(null);
            if (ObjectUtils.isNotEmpty(sub)) {
                map.put("bqnum", sub.get("bqnum"));
                map.put("bqtotal", sub.get("bqtotal"));
                map.put("bqper", sub.get("bqper"));
            } else {
                map.put("bqnum", null);
                map.put("bqtotal", null);
                map.put("bqper", null);
            }


            // 到上期末完成
            BigDecimal meNumMapSum3 = null;
            if (ObjectUtils.isNotEmpty(map.get("bqnum")) && ObjectUtils.isNotEmpty(map.get("bqmnum"))) {
                BigDecimal bqnum = (BigDecimal) map.get("bqnum");
                BigDecimal bqmnum = (BigDecimal) map.get("bqmnum");
                meNumMapSum3 = bqmnum.subtract(bqnum);
            }
            BigDecimal meTotalMapSum3 = null;
            if (ObjectUtils.isNotEmpty(map.get("bqtotal")) && ObjectUtils.isNotEmpty(map.get("bqmtotal"))) {
                BigDecimal bqtotal = (BigDecimal) map.get("bqtotal");
                BigDecimal bqmtotal = (BigDecimal) map.get("bqmtotal");
                meTotalMapSum3 = bqmtotal.subtract(bqtotal);
            }
            map.put("sqmnum", meNumMapSum3);
            map.put("sqmtotal", meTotalMapSum3);

        }

        // 供应商
        MaMaterialSupplier supplierData = getSupplierData(reportId, gysId);
        String typeName = supplierData.getGhcl();

        // 内容
        List<Map<String, Object>> workList = new ArrayList<>();

        Map<String, Object> map1 = new HashMap<>();
        map1.put("manifestCode", null);
        map1.put("manifestName", typeName);
        map1.put("clPlanCgNum", null);
        map1.put("clBeChgNum", null);
        map1.put("clAfChgNum", null);
        map1.put("bqmNum", meNumSum2);
        map1.put("bqmTotal", meTotalSum2);
        map1.put("bqmPerNum", per2);
        map1.put("sqmNum", meNumSum3);
        map1.put("sqmTotal", meTotalSum3);
        map1.put("bqNum", meNumSum1);
        map1.put("bqTotal", meTotalSum1);
        map1.put("bqPerNum", per1);
        workList.add(map1);

        for (Map<String, Object> sub : details2) {
            Map<String, Object> map = new HashMap<>();
            map.put("manifestCode", sub.get("code"));
            map.put("manifestName", sub.get("name"));
            map.put("clPlanCgNum", null);
            map.put("clBeChgNum", null);
            map.put("clAfChgNum", null);
            map.put("bqmNum", sub.get("bqmnum"));
            map.put("bqmTotal", sub.get("bqmtotal"));
            map.put("bqmPerNum", per2);
            map.put("sqmNum", sub.get("sqmnum"));
            map.put("sqmTotal", sub.get("sqmtotal"));
            map.put("bqNum", sub.get("bqnum"));
            map.put("bqTotal", sub.get("bqtotal"));
            map.put("bqPerNum", per1);
            workList.add(map);
        }

        return workList;
    }

    // 配置月度供货
    public List<Map<String, Object>> getYdghData(Long reportId, Long gysId) {
        // 计量单-本期完成
        List<MaMaterialMeasureSingle> currMeasureData = getMeasureData(reportId, gysId, true);
        // 内容
        List<Map<String, Object>> workList = new ArrayList<>();
        for (MaMaterialMeasureSingle datum : currMeasureData) {
            Map<String, Object> workItem = new HashMap<>();
            workItem.put("manifestCode", datum.getManifestCode());
            workItem.put("manifestName", datum.getManifestName());
            workItem.put("number", datum.getNumber());
            workItem.put("marketPrice", datum.getMarketPrice());
            workItem.put("adjustments", datum.getAdjustments());
            workItem.put("settlementPrice", datum.getSettlementPrice());
            workItem.put("total", datum.getTotal());
            workItem.put("acceptanceCode", 	datum.getAcceptanceCode());
            workItem.put("remark", datum.getRemark());
            workList.add(workItem);
        }
        return workList;
    }

    // @TODO 支付统计 暂时放点假数据
    public Map<String, Object> zftj(Long projectId) {
        Map<String, Object> res = new HashMap<>();
        List<Map<String, Object>> data = new ArrayList<>();
        List<Map<String, Object>> header = new ArrayList<>();

        // 供应商 期数 供货周期 材料类别 站点 金额
        // 获取标段
        LambdaQueryWrapper<BaseBsBd> lqw7 = Wrappers.lambdaQuery();
        lqw7.eq(projectId != null, BaseBsBd::getProjectId, projectId);
        List<BaseBsBd> bdList = bdMapper.selectList(lqw7);

        for (BaseBsBd bsBd : bdList) {
            Long bdId = bsBd.getId();

            Map<String, Object> top = new HashMap<>();
            top.put("bdName", bsBd.getName());
            List<Map<String, Object>> zdList = new ArrayList<>();

            LambdaQueryWrapper<BaseBsZd> lqw6 = Wrappers.lambdaQuery();
            lqw6.eq(bdId!= null, BaseBsZd::getBdId, bdId);
            List<BaseBsZd> zds = zdMapper.selectList(lqw6);

            for (int i=0; i< zds.size(); i++) {
                BaseBsZd item = zds.get(i);
                Long zdId = item.getId();

                LambdaQueryWrapper<MaMaterialSupplier> lqw3 = Wrappers.lambdaQuery();
                lqw3.eq(zdId != null, MaMaterialSupplier::getZdId, zdId);
                List<MaMaterialSupplier> suppliers = supplierMapper.selectList(lqw3);

                for (int j=0; j<suppliers.size(); j++) {
                    MaMaterialSupplier supplier = suppliers.get(j);
                    Map<String, Object> zdMap = new HashMap<>();
                    zdMap.put("zdName", item.getName() + "供货金额(元)");
                    zdMap.put("zdVal", supplier.getSupplierId());
                    zdList.add(zdMap);

                    Map<String, Object> map = new HashMap<>();
                    map.put("gysName", supplier.getDeptName());
                    map.put("qhName", "第"+(j +1)+"期");
                    map.put("date", "2024-11-01至2024-11-31");
                    map.put("type", supplier.getGhcl());
                    map.put(supplier.getSupplierId().toString(), BigDecimal.ZERO);

                    data.add(map);
                }
                top.put("zdList", zdList);

            }
            header.add(top);
        }
        res.put("header", header);
        res.put("list", data);
        return res;

    }


}
